'use strict';

import * as moment from 'moment';

function formatNumber(n) {
    var n = n.toString();
    return n[1] ? n : '0' + n;
}

export function relativeTime(time) {
  return moment(new Date(time * 1000)).fromNow();
};

export function formatTime(date,full){
    date =new Date(date);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    if (full) {
        return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
    } else {
        return [year, month, day].map(formatNumber).join('-');
    }
}

export function timeSpace(date){
    let myDate = new Date().getTime();
    let seconds=new Date(date).getTime()/1000;
    let split;
    split=myDate / 1000;
    let cursecds;
    cursecds = parseInt(split,10);
    let difsecds = cursecds - seconds;
    let timeStr;
    if (difsecds / 60 < 5) {
        timeStr = "刚刚";
    } else if (difsecds / 3600 < 1) {
        timeStr = difsecds / 60;
        timeStr = parseInt(timeStr) + "分钟前";
    } else if ((difsecds / 3600 > 1) && (difsecds / 86400 < 1)) {
        timeStr = difsecds / 3600;
        timeStr = parseInt(timeStr) + "小时前";
    } else if (difsecds / 86400 > 1) {
        timeStr = difsecds / 86400;
        if (timeStr < 7) {
            timeStr = parseInt(timeStr) + "天前";
        } else if (timeStr < 14) {
            timeStr = "1周前";
        } else if (timeStr < 21) {
            timeStr = "2周前";
        } else if (timeStr < 28) {
            timeStr = "3周前";
        } else if (timeStr < 60) {
            timeStr = "1个月前";
        } else if (timeStr < 90) {
            timeStr = "2个月前";
        } else if (timeStr < 120) {
            timeStr = "3个月前";
        } else if (timeStr < 150) {
            timeStr = "4个月前";
        } else if (timeStr < 180) {
            timeStr = "5个月前";
        } else if (timeStr < 210) {
            timeStr = "6个月前";
        } else if (timeStr < 240) {
            timeStr = "7个月前";
        } else if (timeStr < 270) {
            timeStr = "8个月前";
        } else if (timeStr < 300) {
            timeStr = "9个月前";
        } else if (timeStr < 330) {
            timeStr = "10个月前";
        } else if (timeStr < 360) {
            timeStr = "11个月前";
        } else {
            timeStr = "1年前";
        }
    }
    return timeStr;
}

export function domain (url) {
  return url && url.split('/')[2];
};
